package org.gnyxy.controller.front;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.context.BaseContext;
import org.gnyxy.dto.AddressDTO;
import org.gnyxy.entity.Address;
import org.gnyxy.result.Result;
import org.gnyxy.service.AddressService;
import org.gnyxy.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userAddressController")
@RequestMapping("/front/api/address")
@Slf4j
@Api(tags = "小程序端-地址管理")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 新增地址
     * @param addressDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增地址")
    public Result add(@RequestBody AddressDTO addressDTO){
        log.info("新增地址: {}",addressDTO);
        addressService.add(addressDTO);
        return Result.success();
    }

    /**
     * 查询该用户所有的地址
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("查询该用户所有的地址")
    public Result<List<AddressVO>> list(){
        log.info("查询该用户所有的地址");
        List<AddressVO> addressVOS = addressService.listByUserId(BaseContext.getCurrentId());
        return Result.success(addressVOS);
    }

    /**
     * 查询默认地址
     * @return
     */
    @PostMapping("/getDefault")
    @ApiOperation("查询默认地址")
    public Result<AddressVO> getDefault(){
        log.info("查询默认地址");
        AddressVO addressVO = addressService.getDefault(BaseContext.getCurrentId());
        return Result.success(addressVO);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<AddressVO> getById(@PathVariable Long id){
        log.info("根据id: {} 查询地址",id);
        AddressVO addressVO = addressService.getById(id);
        return Result.success(addressVO);
    }

    /**
     * 更新
     * @param address
     */
    @PostMapping("/update")
    @ApiOperation("更新地址")
    public Result update(@RequestBody Address address){
        log.info("更新地址: {}",address);
        addressService.update(address);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("删除地址")
    public Result deleteById(@PathVariable Long id){
        log.info("删除地址:{}",id);
        addressService.deleteById(id);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param id
     */
    @PostMapping("/setDefault/{id}")
    @ApiOperation("设置默认地址")
    public Result setDefault(@PathVariable Long id){
        log.info("设置id为:{} 默认地址",id);
        addressService.setDefault(id);
        return Result.success();
    }

}
